package com.xzj.common.core.domain.vo;


import com.xzj.common.core.constants.Constants;
import com.xzj.common.core.enums.BaseEnum;
import lombok.Data;

/**
 * @Category com.xzj.com.xzj.basic.base.dto
 * @Description 【基础统一返回对象】
 * @Author xiaj07
 * @Since 2022/11/17 14:23
 **/
@Data
public class R {

    /**
     * 成功与否标志
     */
    private boolean success = true;

    /**
     * 返回状态码，默认200.前端需要拦截一些常见的状态码如403、404、500等
     */
    private Integer status = 200;

    /**
     * 编码，可用于前端处理多语言，不需要则不用返回编码
     */
    private String code;

    /**
     * 相关消息
     */
    private String msg;

    /**
     * 返回相关数据
     */
    private Object result;

    /**
     * 执行时长，毫秒，通过aop进行计算
     */
    private Long executionTime;

    /**
     * 执行链路Id
     */
    private String traceId;

    public R() {
    }

    public static R success() {
        return new R().putStatus(Constants.SUCCESS);
    }

    public static R fail() {
        R ajaxResult = new R().putStatus(Constants.FAIL);
        ajaxResult.setSuccess(false);
        return ajaxResult;
    }

    public static R success(Object data) {
        return new R().putStatus(Constants.SUCCESS).putResult(data);
    }

    public static R fail(String msg) {
        R ajaxResult = new R().putStatus(Constants.FAIL).putMsg(msg);
        ajaxResult.setSuccess(false);
        return ajaxResult;
    }

    public R putStatus(Integer status) {
        this.status = status;
        return this;
    }

    public R putCode(String code) {
        this.code = code;
        this.status = Integer.parseInt(code);
        return this;
    }

    public R putMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public R putResult(Object result) {
        this.result = result;
        return this;
    }

    public R putExecutionTime(long executionTime) {
        this.executionTime = executionTime;
        return this;
    }

    public R putTraceId(String traceId) {
        this.traceId = traceId;
        return this;
    }

    public R putEnum(BaseEnum baseEnum) {
        this.code = baseEnum.getCode();
        this.status = Integer.parseInt(baseEnum.getCode());
        this.msg = baseEnum.getDesc();
        return this;
    }

}
